package com.xiguanlezz.cn.common.remote.client.grpc;

import lombok.Builder;
import lombok.Setter;
import lombok.experimental.Accessors;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @Author：习惯了种种
 * @CreateDate：2024/11/19 15:46
 * @Description：GRPC客户端配置类
 */
@Setter
public class DefaultGrpcClientConfig implements GrpcClientConfig {
    private static final int CPU_PROCESSORS_NUMBER = Runtime.getRuntime().availableProcessors();

    private String name;
    private Map<String, String> labels = new HashMap<>();

    private int retryTimes = 3; /* 请求失败的重试次数 */
    private long timeOutMills = 3000L; /* 超时时间 */
    private long connectionKeepAlive = 5000L; /* 连接探活的间隔 */
    private long threadPoolKeepAlive = 10000L; /* 线程池中线程的存活时间 */
    private int threadPoolCoreSize = 2 * CPU_PROCESSORS_NUMBER; /* 线程池中核心线程的数量 */
    private int threadPoolMaxSize = 8 * CPU_PROCESSORS_NUMBER; /* 线程池中线程的最大数量 */
    private long serverCheckTimeOut = 3000L; /* 客户端探测服务端的超时时间 */
    private int threadPoolQueueSize = 10000; /* 线程池中队列的容量 */
    private int maxInboundMessageSize = 10 * 1024 * 1024; /* 入站消息的最大容量 */
    private int channelKeepAlive = 6 * 60 * 1000; /* channel保活的间隔 */
    private long channelKeepAliveTimeout = TimeUnit.SECONDS.toMillis(20L); /* channel保活的超时时间 */
    private int healthCheckRetryTimes = 3; /* 客户端探测服务端的重试次数 */
    private long healthCheckTimeOut = 3000L; /* 客户端探测服务端的超时时间 */
    private long capabilityNegotiationTimeout = 5000L; /* 能力点协商的超时时间 */


    public DefaultGrpcClientConfig(String name) {
        this.name = name;
    }

    public DefaultGrpcClientConfig(String name, Map<String, String> labels) {
        this.name = name;
        this.labels = labels;
    }

    @Override
    public int threadPoolCoreSize() {
        return threadPoolCoreSize;
    }

    @Override
    public int threadPoolMaxSize() {
        return threadPoolMaxSize;
    }

    @Override
    public long threadPoolKeepAlive() {
        return threadPoolKeepAlive;
    }

    @Override
    public long serverCheckTimeOut() {
        return serverCheckTimeOut;
    }

    @Override
    public int threadPoolQueueSize() {
        return threadPoolQueueSize;
    }

    @Override
    public int maxInboundMessageSize() {
        return maxInboundMessageSize;
    }

    @Override
    public int channelKeepAlive() {
        return channelKeepAlive;
    }

    @Override
    public long channelKeepAliveTimeout() {
        return channelKeepAliveTimeout;
    }

    @Override
    public long capabilityNegotiationTimeout() {
        return capabilityNegotiationTimeout;
    }

    @Override
    public String name() {
        return name;
    }

    @Override
    public int retryTimes() {
        return retryTimes;
    }

    @Override
    public long timeOutMills() {
        return timeOutMills;
    }

    @Override
    public long connectionKeepAlive() {
        return connectionKeepAlive;
    }

    @Override
    public int healthCheckRetryTimes() {
        return healthCheckRetryTimes;
    }

    @Override
    public long healthCheckTimeOut() {
        return healthCheckTimeOut;
    }

    @Override
    public Map<String, String> labels() {
        return labels;
    }

}
